package leetcode;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by MtRider on 2021/10/25 20:40
 */
public class FirstUniqChar {
    public static void main(String[] args) {
        System.out.println(firstUniqChar("loveleetcode"));
    }

    public static int firstUniqChar(String s) {
        char[] chars = s.toCharArray();
        HashMap<Character, Integer> map = new HashMap<>();
        for (int i = 0; i < chars.length; i++) {
            if (map.get(chars[i]) == null) {
                map.put(chars[i], 1);
            } else {
                map.put(chars[i], map.get(chars[i])+1);
            }
        }
        for (int i = 0; i < chars.length; i++) {
            for (Map.Entry<Character, Integer> entry : map.entrySet()) {
                if(chars[i] == entry.getKey()&& entry.getValue()==1){
                    return i;
                }
            }
        }
        return -1;
    }
}
